{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from pandas.api.types import CategoricalDtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>门店编号</th>\n",
       "      <th>全年销售额</th>\n",
       "      <th>门店类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>696</td>\n",
       "      <td>普通店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>830</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>941</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>860</td>\n",
       "      <td>普通店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>609</td>\n",
       "      <td>商超店</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   门店编号  全年销售额 门店类型\n",
       "0     1    696  普通店\n",
       "1     2    830  旗舰店\n",
       "2     3    941  旗舰店\n",
       "3     4    860  普通店\n",
       "4     5    609  商超店"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取数据集\n",
    "df = pd.read_excel('./示例文件.xlsx',sheet_name='data')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['旗舰店', 'SPM店', '商超店', '普通店', '街边店']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一\n",
    "# 自定义列表\n",
    "category_df = pd.read_excel('./示例文件.xlsx',sheet_name='list',header=None)\n",
    "category_list = category_df[0].tolist()\n",
    "category_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>门店编号</th>\n",
       "      <th>全年销售额</th>\n",
       "      <th>门店类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>830</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>941</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>314</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>480</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>619</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199</th>\n",
       "      <td>200</td>\n",
       "      <td>791</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>200</th>\n",
       "      <td>201</td>\n",
       "      <td>502</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>214</th>\n",
       "      <td>215</td>\n",
       "      <td>689</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>215</th>\n",
       "      <td>216</td>\n",
       "      <td>938</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>218</th>\n",
       "      <td>219</td>\n",
       "      <td>455</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>220 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     门店编号  全年销售额 门店类型\n",
       "1       2    830  旗舰店\n",
       "2       3    941  旗舰店\n",
       "9      10    314  旗舰店\n",
       "15     16    480  旗舰店\n",
       "23     24    619  旗舰店\n",
       "..    ...    ...  ...\n",
       "199   200    791  街边店\n",
       "200   201    502  街边店\n",
       "214   215    689  街边店\n",
       "215   216    938  街边店\n",
       "218   219    455  街边店\n",
       "\n",
       "[220 rows x 3 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按 门店类型 升序排序\n",
    "df['门店类型'] = df['门店类型'].astype('category')\n",
    "df['门店类型'].cat.set_categories(category_list, inplace=True)\n",
    "df.sort_values(by=['门店类型','门店编号'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>旗舰店</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SPM店</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商超店</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>普通店</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>街边店</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      index\n",
       "0          \n",
       "旗舰店       0\n",
       "SPM店      1\n",
       "商超店       2\n",
       "普通店       3\n",
       "街边店       4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法二\n",
    "# 自定义列表\n",
    "sort_mapping = category_df[0].reset_index().set_index(0)\n",
    "sort_mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>门店编号</th>\n",
       "      <th>全年销售额</th>\n",
       "      <th>门店类型</th>\n",
       "      <th>辅助列</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>696</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>830</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>941</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>860</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>609</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>215</th>\n",
       "      <td>216</td>\n",
       "      <td>938</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>216</th>\n",
       "      <td>217</td>\n",
       "      <td>753</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>217</th>\n",
       "      <td>218</td>\n",
       "      <td>109</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>218</th>\n",
       "      <td>219</td>\n",
       "      <td>455</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>219</th>\n",
       "      <td>220</td>\n",
       "      <td>803</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>220 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     门店编号  全年销售额 门店类型 辅助列\n",
       "0       1    696  普通店   3\n",
       "1       2    830  旗舰店   0\n",
       "2       3    941  旗舰店   0\n",
       "3       4    860  普通店   3\n",
       "4       5    609  商超店   2\n",
       "..    ...    ...  ...  ..\n",
       "215   216    938  街边店   4\n",
       "216   217    753  商超店   2\n",
       "217   218    109  普通店   3\n",
       "218   219    455  街边店   4\n",
       "219   220    803  商超店   2\n",
       "\n",
       "[220 rows x 4 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 添加辅助列\n",
    "df['辅助列'] = df['门店类型'].map(sort_mapping['index'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>门店编号</th>\n",
       "      <th>全年销售额</th>\n",
       "      <th>门店类型</th>\n",
       "      <th>辅助列</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>830</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>941</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>314</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>480</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>619</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199</th>\n",
       "      <td>200</td>\n",
       "      <td>791</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>200</th>\n",
       "      <td>201</td>\n",
       "      <td>502</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>214</th>\n",
       "      <td>215</td>\n",
       "      <td>689</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>215</th>\n",
       "      <td>216</td>\n",
       "      <td>938</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>218</th>\n",
       "      <td>219</td>\n",
       "      <td>455</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>220 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     门店编号  全年销售额 门店类型 辅助列\n",
       "1       2    830  旗舰店   0\n",
       "2       3    941  旗舰店   0\n",
       "9      10    314  旗舰店   0\n",
       "15     16    480  旗舰店   0\n",
       "23     24    619  旗舰店   0\n",
       "..    ...    ...  ...  ..\n",
       "199   200    791  街边店   4\n",
       "200   201    502  街边店   4\n",
       "214   215    689  街边店   4\n",
       "215   216    938  街边店   4\n",
       "218   219    455  街边店   4\n",
       "\n",
       "[220 rows x 4 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按 门店类型 升序排序\n",
    "df.sort_values(by=['辅助列','门店编号'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      普通店\n",
       "1      旗舰店\n",
       "2      旗舰店\n",
       "3      普通店\n",
       "4      商超店\n",
       "      ... \n",
       "215    街边店\n",
       "216    商超店\n",
       "217    普通店\n",
       "218    街边店\n",
       "219    商超店\n",
       "Name: 门店类型_2, Length: 220, dtype: category\n",
       "Categories (5, object): ['旗舰店' < 'SPM店' < '商超店' < '普通店' < '街边店']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法三\n",
    "# 使用 CategoricalDtype\n",
    "cat_size_order = CategoricalDtype(\n",
    "    category_list, \n",
    "    ordered=True\n",
    ")\n",
    "\n",
    "df['门店类型_2'] = df['门店类型'].astype(cat_size_order)\n",
    "df['门店类型_2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>门店编号</th>\n",
       "      <th>全年销售额</th>\n",
       "      <th>门店类型</th>\n",
       "      <th>辅助列</th>\n",
       "      <th>门店类型_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>830</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>941</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>314</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>480</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>619</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "      <td>旗舰店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199</th>\n",
       "      <td>200</td>\n",
       "      <td>791</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>200</th>\n",
       "      <td>201</td>\n",
       "      <td>502</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>214</th>\n",
       "      <td>215</td>\n",
       "      <td>689</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>215</th>\n",
       "      <td>216</td>\n",
       "      <td>938</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>218</th>\n",
       "      <td>219</td>\n",
       "      <td>455</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "      <td>街边店</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>220 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     门店编号  全年销售额 门店类型 辅助列 门店类型_2\n",
       "1       2    830  旗舰店   0    旗舰店\n",
       "2       3    941  旗舰店   0    旗舰店\n",
       "9      10    314  旗舰店   0    旗舰店\n",
       "15     16    480  旗舰店   0    旗舰店\n",
       "23     24    619  旗舰店   0    旗舰店\n",
       "..    ...    ...  ...  ..    ...\n",
       "199   200    791  街边店   4    街边店\n",
       "200   201    502  街边店   4    街边店\n",
       "214   215    689  街边店   4    街边店\n",
       "215   216    938  街边店   4    街边店\n",
       "218   219    455  街边店   4    街边店\n",
       "\n",
       "[220 rows x 5 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按 门店类型 升序排序\n",
    "df.sort_values(by=['门店类型_2','门店编号'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>门店编号</th>\n",
       "      <th>全年销售额</th>\n",
       "      <th>门店类型</th>\n",
       "      <th>辅助列</th>\n",
       "      <th>门店类型_2</th>\n",
       "      <th>辅助列_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>696</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>830</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>941</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "      <td>旗舰店</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>860</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>609</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>215</th>\n",
       "      <td>216</td>\n",
       "      <td>938</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>216</th>\n",
       "      <td>217</td>\n",
       "      <td>753</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>217</th>\n",
       "      <td>218</td>\n",
       "      <td>109</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "      <td>普通店</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>218</th>\n",
       "      <td>219</td>\n",
       "      <td>455</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "      <td>街边店</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>219</th>\n",
       "      <td>220</td>\n",
       "      <td>803</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "      <td>商超店</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>220 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     门店编号  全年销售额 门店类型 辅助列 门店类型_2  辅助列_2\n",
       "0       1    696  普通店   3    普通店      3\n",
       "1       2    830  旗舰店   0    旗舰店      0\n",
       "2       3    941  旗舰店   0    旗舰店      0\n",
       "3       4    860  普通店   3    普通店      3\n",
       "4       5    609  商超店   2    商超店      2\n",
       "..    ...    ...  ...  ..    ...    ...\n",
       "215   216    938  街边店   4    街边店      4\n",
       "216   217    753  商超店   2    商超店      2\n",
       "217   218    109  普通店   3    普通店      3\n",
       "218   219    455  街边店   4    街边店      4\n",
       "219   220    803  商超店   2    商超店      2\n",
       "\n",
       "[220 rows x 6 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以生成 辅助列\n",
    "df['辅助列_2'] = df['门店类型_2'].cat.codes\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 220 entries, 0 to 219\n",
      "Data columns (total 6 columns):\n",
      " #   Column  Non-Null Count  Dtype   \n",
      "---  ------  --------------  -----   \n",
      " 0   门店编号    220 non-null    int64   \n",
      " 1   全年销售额   220 non-null    int64   \n",
      " 2   门店类型    220 non-null    category\n",
      " 3   辅助列     220 non-null    category\n",
      " 4   门店类型_2  220 non-null    category\n",
      " 5   辅助列_2   220 non-null    int8    \n",
      "dtypes: category(3), int64(2), int8(1)\n",
      "memory usage: 5.0 KB\n"
     ]
    }
   ],
   "source": [
    "# 查看一下数据表的信息\n",
    "df.info()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
